/**
 * Created by hongye.liu on 14-11-7.
 */
define(['jquery','underscore','backbone','global','text!/android_asset/busAppWeb/module/busApp/busSiteAdd/busSiteAdd.html'],
    function($,_,Backbone,global,html){
    var busSiteAddView =  Backbone.View.extend({
        el:$('#busMain'),
        siteSet:[],
        template: _.template(html),
        initialize:function(data){
            this.render();
            this.addEvent();
        },
        render:function(){
            $(this.el).html(this.template);
            $('#busMain').trigger('create');
            this.init();
        },
        addEvent:function(){
            var _that = this;
            $('#sureBtn').click(function(){
                _that.sureClick();
            })
        },

        sureClick:function(){
            var siteSetObject = new Object();
            siteSetObject.value = document.getElementById('siteRemind').value;
            siteSetObject.pointX = this.siteSet[siteSetObject.value].lat;
            siteSetObject.pointY = this.siteSet[siteSetObject.value].lng;
            siteSetObject.note = document.getElementById('note').value;
            siteSetObject.isRemind = document.getElementById('switch').value;
            global.userSiteObject.push(siteSetObject);
            Backbone.history.navigate('#busRemind', true);
        },

        init:function(){
            var acSite = new BMap.Autocomplete(
                {
                    "input" : "siteRemind"
                    ,"location" : global.map
                });
            this.setAutoComplete(acSite);
        },
        setAutoComplete:function(ac){
            ac.addEventListener("onhighlight", function(e) {
                var str = "";
                var _value = e.fromitem.value;
                var value = "";
                if (e.fromitem.index > -1) {
                    value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
                }
                str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;

                value = "";
                if (e.toitem.index > -1) {
                    _value = e.toitem.value;
                    value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
                }
                str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
                $('#searchResultPanel').innerHTML = str;
            });

            var myValue;
            var _that = this;
            ac.addEventListener("onconfirm", function(e) {
                var _value = e.item.value;
                myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
                $("#searchResultPanel").innerHTML ="onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;

                _that.getPoint(myValue);
            });
        },
        getPoint:function(site) {
            this.map.clearOverlays();
            var _this = this;
            function myFun() {
                var point = local.getResults().getPoi(0).point;
                _this.siteSet[siteDetail] = point;
            }
            var local = new BMap.LocalSearch(global.map, {
                onSearchComplete: myFun
            });
            local.search(siteDetail);


        }
    });
        return busSiteAddView;
})